package com.application.gas;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.*;
import com.application.Formula;
import com.application.R;
import util.exceptions.InvalidDataException;
import util.tools.etc.Misc;
import util.tools.gas.GasData;
import util.tools.gas.GasLaws;

import java.util.ArrayList;

public class IdealGases extends Activity {
    public String P_units = "kPa", V_units = "L", T_units = "C";
    private int current;
    private static long last = 0;


    public void trySolve() {
        if (System.currentTimeMillis() - last < 10) {
            last = System.currentTimeMillis();
            return;
        }
        last = System.currentTimeMillis();
        GasData use;
        String pressure, volume, number, temperature;
        pressure = getEditText(R.id.formula_A_text);
        volume = getEditText(R.id.formula_A2_text);
        number = getEditText(R.id.formula_B_text);
        temperature = getEditText(R.id.formula_B2_text);

        try {
            double a = Misc.getValue(pressure);
            double b = Misc.getValue(volume);
            double c = Misc.getValue(number);
            double d = Misc.getValue(temperature);

            if (a != -1)
                a = GasLaws.convertPressure(a, P_units);
            if (b != -1)
                b = GasLaws.convertVolume(b, V_units);
            if (d != -1)
                d = GasLaws.convertTemperature(d, T_units);

            if (current == R.id.formula_A_text)
                a = -1;
            else if (current == R.id.formula_A2_text)
                b = -1;
            else if (current == R.id.formula_B_text)
                c = -1;
            else if (current == R.id.formula_B2_text)
                d = -1;

            use = new GasData(a, b, c, d);
            int id = -1;
            if (a == -1 || current == R.id.formula_A_text)
                id = R.id.formula_A_text;
            else if (b == -1 || current == R.id.formula_A2_text)
                id = R.id.formula_A2_text;
            else if (c == -1 || current == R.id.formula_B_text)
                id = R.id.formula_B_text;
            else if (d == -1 || current == R.id.formula_B2_text)
                id = R.id.formula_B2_text;
            if (id != -1) {
                double value = GasLaws.ideal(use);
                switch (current) {
                    case R.id.formula_A_text:
                        value = GasLaws.convertBackPressure(value, P_units);
                        break;
                    case R.id.formula_A2_text:
                        value = GasLaws.convertBackVolume(value, V_units);
                        break;
                    case R.id.formula_B2_text:
                        value = GasLaws.convertBackTemperature(value, T_units);
                }
                String result = Misc.round(value);
                changeText(id, result);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } catch (InvalidDataException e) {
            changeText(current, "");
        }
    }

    private String getEditText(int resourceId) {
        return ((EditText) findViewById(resourceId)).getText().toString();
    }

    private void changeText(int resourceId, String s) {
        if (!(findViewById(resourceId) instanceof EditText))
            return;
        EditText ed = ((EditText) findViewById(resourceId));
        if (ed.getText().toString().equals(s))
            return;
        ((EditText) findViewById(resourceId)).setText(s);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.formula_4);
        current = R.id.formula_A_text;

        View[] radio = new View[4];
        ArrayList<View> ids = findViewById(R.id.formula_radio_group_solve).getTouchables();
        View[] text = new View[4];
        text[0] = findViewById(R.id.formula_A_text);
        text[1] = findViewById(R.id.formula_A2_text);
        text[2] = findViewById(R.id.formula_B_text);
        text[3] = findViewById(R.id.formula_B2_text);

        Spinner[] gen = new Spinner[3];
        gen[0] = (Spinner) findViewById(R.id.formula_A_spinner);
        gen[1] = (Spinner) findViewById(R.id.formula_A2_spinner);
        gen[2] = (Spinner) findViewById(R.id.formula_B2_spinner);
        char[] type = {'p', 'v', 't'};
        Formula f = new Formula(ids.toArray(radio), text, gen, type, this) {
            @Override
            public void act(int i, Spinner c) {
                if (i == 0) {
                    P_units = c.getSelectedItem().toString();
                } else if (i == 1) {
                    V_units = c.getSelectedItem().toString();
                } else if (i == 2) {
                    T_units = c.getSelectedItem().toString().substring(1);
                }
                current = getCurrent();
                trySolve();
            }

            @Override
            public void act(int cur) {
                current = cur;
                trySolve();
            }
        };
        findViewById(R.id.formula_B_spinner).setVisibility(View.GONE);
        findViewById(R.id.formula_B_view).setVisibility(View.VISIBLE);
        RadioGroup.OnCheckedChangeListener checkListener = f.getCheckedChangeListener();
        ((RadioGroup) findViewById(R.id.formula_radio_group_solve)).setOnCheckedChangeListener(checkListener);

        ((TextView) findViewById(R.id.formula_B2_label)).setLineSpacing(1.5f, 1.5f);
        ((TextView) findViewById(R.id.formula_B_label)).setLineSpacing(1.5f, 1.5f);
        ((TextView) findViewById(R.id.formula_A2_label)).setLineSpacing(1.5f, 1.5f);
        ((TextView) findViewById(R.id.formula_A_label)).setLineSpacing(1.5f, 1.5f);

        ((TextView) findViewById(R.id.formula_A2_label)).setText("V");
        ((TextView) findViewById(R.id.formula_A_label)).setText("P");
        ((TextView) findViewById(R.id.formula_B2_label)).setText("T");
        ((TextView) findViewById(R.id.formula_B_label)).setText("n");

        ((RadioButton) findViewById(R.id.formula_radio_A2)).setText("V");
        ((RadioButton) findViewById(R.id.formula_radio_A)).setText("P");
        ((RadioButton) findViewById(R.id.formula_radio_B2)).setText("T");
        ((RadioButton) findViewById(R.id.formula_radio_B)).setText("n");

    }
}